package com.syni.mdd.yhd.common.all.quartz.groupBuy.job;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrderStatus;
import com.syni.mdd.yhd.common.all.quartz.order.job.OrderCancelTimingJob;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderStatusService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @className GroupMessageTimingInit
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/8 18:16
 * @VERSION 1.0
 **/
@Component
@DependsOn("applicationContextHolder")
public class LimitGroupBuyInit {
    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Autowired
    BmsDxOrderStatusService bmsDxOrderStatusService;

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    @PostConstruct
    public void init(){
        //查询所有未支付的订单id
        Integer pageNum = 1;
        Integer pageSize = 100;
        Map<String,Object> conditionMap = new HashMap<>();
        Set<Integer> statuses = new HashSet<>();
        statuses.add(Constants.NOT_ONLINE_STATUS);
        statuses.add(Constants.ONLINE_STATUS);
        conditionMap.put("status_IN",statuses);
        conditionMap.put("isTimeLimit",Constants.COMMON_FLAG_TRUE_INT);
        while (true) {
            Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize);
            List<BmsDxGroupBuy> bmsDxGroupBuys = bmsDxGroupBuyService.getBaseDao().findAll(new SpecificationUtils(conditionMap),pageable).getContent();
            if(bmsDxGroupBuys != null && bmsDxGroupBuys.size() > 0){
                for(BmsDxGroupBuy bmsDxGroupBuy : bmsDxGroupBuys){
                    bmsDxGroupBuyService.doGroupBuyTiming(LimitGroupBuyTimingJob.class,bmsDxGroupBuy);
                }
                pageNum++;
            }else {
                break;
            }
        }
    }
}
